package org.jeecg.modules.business.service.impl;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.ibatis.annotations.Select;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.TokenUtils;
import org.jeecg.modules.business.dto.CouponsGenerateDto;
import org.jeecg.modules.business.entity.BusMarketCouponsCash;
import org.jeecg.modules.business.entity.BusMarketCouponsCashUsed;
import org.jeecg.modules.business.entity.BusMarketMealCoupons;
import org.jeecg.modules.business.entity.BusMarketMealCouponsUsed;
import org.jeecg.modules.business.enums.CouponsStatusEnum;
import org.jeecg.modules.business.mapper.BusMarketCouponsCashMapper;
import org.jeecg.modules.business.mapper.BusMarketMealCouponsMapper;
import org.jeecg.modules.business.mapper.BusMarketMealCouponsUsedMapper;
import org.jeecg.modules.business.service.IBusMarketMealCouponsUsedService;
import org.jeecg.modules.business.vo.MealCouponStatVo;
import org.jeecg.modules.business.vo.MemberCardStatVo;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.UUID;

/**
 * @Description: bus_market_meal_coupons_used
 * @Author: jeecg-boot
 * @Date:   2023-03-14
 * @Version: V1.0
 */
@Service
public class BusMarketMealCouponsUsedServiceImpl extends ServiceImpl<BusMarketMealCouponsUsedMapper, BusMarketMealCouponsUsed> implements IBusMarketMealCouponsUsedService {
    @Resource
    private BusMarketMealCouponsMapper busMarketMealCouponsMapper;
    @Resource
    private BusMarketMealCouponsUsedMapper busMarketMealCouponsUsedMapper;
    /**
     * 生成16位数字+prefix
     * @param prefix
     * @return
     */
    private String randomNumber(String prefix) {
        int first = new Random(10).nextInt(8) + 1;
        int hashCode = UUID.randomUUID().toString().hashCode();
        if (hashCode < 0) {
            hashCode = -hashCode;
        }
        return prefix + first + String.format("%015d", hashCode);
    }
    /**
     * 生成餐券领取使用
     * @param dto
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Result generate(CouponsGenerateDto dto) {
        if (ObjectUtils.isEmpty(dto.getCouponsId())) return Result.error("餐券id不能为空");
        if (dto.getNum() <= 0) return Result.error("生成数量不能小于1!");
        BusMarketMealCoupons coupons = busMarketMealCouponsMapper.selectById(dto.getCouponsId());
        if (ObjectUtils.isEmpty(coupons)) {
            return Result.error("餐券活动不存在");
        }
        List<BusMarketMealCouponsUsed> busMarketMealCouponsUsedList = new ArrayList<>();
        while (dto.getNum() > 0) {
            BusMarketMealCouponsUsed busMarketMealCouponsUsed = new BusMarketMealCouponsUsed();
            busMarketMealCouponsUsed.setTenantId(TokenUtils.currentTenantId());
            busMarketMealCouponsUsed.setHotelId(coupons.getHotelId());
            busMarketMealCouponsUsed.setCouponsId(dto.getCouponsId());
            busMarketMealCouponsUsed.setCode(randomNumber("CJ"));
            busMarketMealCouponsUsed.setStatus(0);
            busMarketMealCouponsUsed.setCreateTime(DateTime.now());
            busMarketMealCouponsUsedList.add(busMarketMealCouponsUsed);
            dto.setNum(dto.getNum() - 1);
        }
        this.saveBatch(busMarketMealCouponsUsedList);
        return Result.ok("创建成功");
    }



    /**
     * 已核销餐券列表
     * @param hotelId
     * @param type
     * @return
     */
    public Page<BusMarketMealCouponsUsed> verifyList(Page<BusMarketMealCouponsUsed> page, String hotelId,String type) {
        return page.setRecords(busMarketMealCouponsUsedMapper.verifyList(page, hotelId, CouponsStatusEnum.used.getKey(),type));
    }

    /**
     * 统计
     * @param hotelId
     * @return
     */
    public MealCouponStatVo stat(String hotelId,String type) {
        return busMarketMealCouponsUsedMapper.stat(hotelId, type);
    }
}
